    //
    //  HWTextStepper.m
    //  HWTextStepperDemo
    //
    //  Created by LiTengFei on 15/3/9.
    //  Copyright (c) 2015年 LiTengFei. All rights reserved.
    //

#import "HWTextStepper.h"
@interface HWTextStepper (){
    UIButton * addButton;
    UIButton * subtractButton;
    UITextField * valueField;
}
@end

static CGFloat button_width = 40;
static CGFloat button_height = 40;
    //static CGFloat button_
@implementation HWTextStepper

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = CGRectGetWidth(frame);
        CGFloat height = CGRectGetHeight(frame);
        CGFloat mix1 = 0;
        CGFloat mix2 = 0;

            mix1 = height * 0.8;


            mix2 = width / 3;


        button_height = button_width = MIN(mix1, mix2);

        self.valueFieldEditable = NO;

        [self commonInstance];
    }
    return self;
}
-(void)commonInstance{

    addButton = [[UIButton alloc]init];
    addButton.layer.cornerRadius = button_width / 2;
    addButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    addButton.layer.borderWidth = 1;
    [addButton setTitle:@"+" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    subtractButton = [[UIButton alloc]init];
    subtractButton.layer.cornerRadius = button_width / 2;
    subtractButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    subtractButton.layer.borderWidth = 1;
    [subtractButton setTitle:@"-" forState:UIControlStateNormal];
    [subtractButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [subtractButton addTarget:self action:@selector(subtractButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    valueField = [[UITextField alloc]init];
    valueField.textAlignment = NSTextAlignmentCenter;
    valueField.enabled = self.valueFieldEditable;

    [self addSubview:addButton];
    [self addSubview:subtractButton];
    [self addSubview:valueField];

    [self dataInstance];
}
-(void)setValueFieldEditable:(BOOL)valueFieldEditable{
    _valueFieldEditable = valueFieldEditable;
    if(valueFieldEditable){
        valueField.enabled = YES;
    }else{
        valueField.enabled = NO;
    }
}
-(void)dataInstance{
    _minValue = 0 ;
    _maxValue = NSIntegerMax;
    valueField.text = [NSString stringWithFormat:@"%ld",_integerValue];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat top_space = (CGRectGetHeight(self.frame) - button_height) / 2;
    CGRect leftButtonFrame = CGRectMake(5, top_space, button_width , button_height);

    CGRect rightButtonFrame = CGRectMake(CGRectGetWidth(self.frame) - 5 - button_width, top_space, button_width, button_height);

    CGFloat value_field_width  = CGRectGetWidth(self.frame) - ( 5 + button_width ) * 2;
    CGRect centerTextFieldFrame = CGRectMake(CGRectGetMaxX(leftButtonFrame), top_space , value_field_width , button_height);

    subtractButton.frame = leftButtonFrame;
    addButton.frame = rightButtonFrame;
    valueField.frame = centerTextFieldFrame;
}

#pragma mark- event 

-(void)addButtonClick:(UIButton *)button{
    NSInteger index = _integerValue;
    index ++;
    if(index <= _maxValue){
        valueField.text = [NSString stringWithFormat:@"%ld",index];
        _integerValue = index;
        [self valueChanged];
    }
}

-(void)subtractButtonClick:(UIButton *)button{
    NSInteger index = _integerValue;
    index -- ;
    if(index >= _minValue){
        valueField.text = [NSString stringWithFormat:@"%ld",index];
        _integerValue = index;
        [self valueChanged];
    }
}

-(void)valueChanged{
    if(self.delegate && [self.delegate respondsToSelector:@selector(textStepper:didValueChanged:)]){
        [self.delegate textStepper:self didValueChanged:self.integerValue];
    }
}
@end
